﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Time;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

using GadgeteerLibrary;

namespace BluetoothHost
{
    public partial class Program
    {
        private GT.Timer connectTimer;
        private GT.Timer heartBeatTimer;
        private RESTClient restClient;
        private static bool timeSynchronized = false;
        private bool hasConnected = false;

        private const byte NOTE_SIZE = 2;
        private const int CONNECT_INTERVAL = 600000;
        private const int HEARTBEAT_INTERVAL = 300000;
        private const int MEASUREMENT_INTERVAL = 4000;
        private const int SEND_INTERVAL = 8000;
        private const short FAULT_TOLERANCE = 60;
        private const string TARGET_SSID = "Connectify-Zheng";
        private const string PASSWORD = "gadg3t33r";

        Program()
        {
            string apiKey = "72b2480d-6ff2-436f-be82-6560138ad24e";
            Device device = new Device(2065, "BluetoothHost");
            ArrayList sensors = new ArrayList();
            sensors.Add(new Sensor("GPS", SensorType.gps, "Seeed", MEASUREMENT_INTERVAL, SEND_INTERVAL, FAULT_TOLERANCE));
            restClient = new RESTClient(apiKey, device, sensors);
        }

        void ProgramStarted()
        {
            Debug.Print("Program Started");
            this.InitializeBluetoothButton();
        }

        void InitializeBluetoothButton()
        {
            button.ButtonPressed += button_ButtonPressed;
            bluetooth.BluetoothStateChanged += bluetooth_BluetoothStateChanged;
            bluetooth.DataReceived += bluetooth_DataReceived;
            bluetooth.DeviceInquired += bluetooth_DeviceInquired;

            Debug.Print("Ready to inquire the device");
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            bluetooth.HostMode.InquireDevice();
        }

        void bluetooth_BluetoothStateChanged(Bluetooth sender, Bluetooth.BluetoothState btState)
        {
            if (btState == Bluetooth.BluetoothState.Connected)
            {
                Debug.Print("Bluetooth Connected");
                if (!hasConnected)
                {
                    hasConnected = true;
                }
            }
            else
            {
                Debug.Print("Bluetooth Not Connected");
                if (hasConnected)
                {
                    this.Alarm();
                    hasConnected = false;
                }
            }
        }

        void bluetooth_DeviceInquired(Bluetooth sender, string macAddress, string name)
        {
            if (name.IndexOf("2089") > -1)
            {
                Debug.Print("Connecting to: " + name);
                bluetooth.HostMode.Connect(macAddress);
            }
        }

        void bluetooth_DataReceived(Bluetooth sender, string data)
        {
            Debug.Print("Received: " + data);
        }

        void Alarm()
        {
            // Alarm Sound
            for (int i = 0; i < NOTE_SIZE; ++i)
            {
                PlayNote(Tunes.Tone.C4, 200);
                PlayNote(Tunes.Tone.G4, 200);
            }

            tunes.Play();
        }

        void PlayNote(Tunes.Tone tone, int duration)
        {
            Tunes.MusicNote note = new Tunes.MusicNote(tone, duration);
            tunes.AddNote(note);
        }
    }
}
